package com.uglive.cg.ui;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.uglive.cg.Values;
import com.uglive.cg.bean.Goods;
import com.uglive.cg.device.qrAndBar.DH8600ScanQrAndBar;
import com.uglive.cg.model.ApiModel;
import com.uglive.cg.model.ChangKuModel;



public class MainPresenter implements MainContract.Presenter {

    private DH8600ScanQrAndBar mScanBarCode = new DH8600ScanQrAndBar();
    private ApiModel mApiModel = new ApiModel();
    private ChangKuModel mChangKuModel = new ChangKuModel();
    private MainContract.View mView;

    public MainPresenter(MainContract.View view) {
        this.mView = view;
    }

    @Override
    public void start() {
        mScanBarCode.open();
        Values.userPhoneNumber = SPUtils.getInstance().getString(Values.SP_CURRENT_USER, null);
        if (TextUtils.isEmpty(Values.userPhoneNumber)) {
            mView.showLoinAlert();
            mView.setUserInfo("请登录");
        } else {
            mView.setUserInfo(Values.userPhoneNumber);
        }
        getCkList();
    }

    @Override
    public void close() {
        mScanBarCode.close();
    }

    @Override
    public void login(String phoneNumber) {
        mView.showProcess("登录中......");
        mApiModel.<String>login(
                phoneNumber,
                () -> mView.dismissProcess(),
                result -> {
                    if (result.equals("\"true\"")) {
                        SPUtils.getInstance().put(Values.SP_CURRENT_USER, phoneNumber);
                        Values.userPhoneNumber = phoneNumber;
                        mView.setUserInfo(phoneNumber);
                        mView.dismissProcess();
                        mView.dismissLoginAlert();
                    } else {
                        ToastUtils.showShort("该号码未录入系统");
                    }
                    mView.dismissProcess();
                });
    }

    @Override
    public void ruku(String num, String barCode, String place) {
        mView.showProcess("入库中......");
        mApiModel.ruku(
                num, barCode, place, Values.userPhoneNumber,
                () -> {
                    mView.dismissProcess();
                    mView.showMessage("操作失败");
                },
                result -> {
                    if (null != result) {
                        Goods goods= JSON.parseObject(result,Goods.class);
                        mView.showGoodsInfo(goods);
                        if(TextUtils.isEmpty(goods.getGoodsName())){
                            ToastUtils.showShort("无法找到货物，请扫描正确的条码");
                        }else {
                            mView.showMessage("操作成功");
                        }
                        mView.dismissProcess();
                    } else {
                        ToastUtils.showShort("服务错误");
                    }
                });
    }

    @Override
    public void chuku(String num, String barCode, String place) {
        mView.showProcess("出库中......");
        mApiModel.chuku(num, barCode, place, Values.userPhoneNumber,
                () -> {
                    mView.dismissProcess();
                    mView.showMessage("操作失败");
                },
                result -> {
                    if (null != result) {
                        Goods goods= JSON.parseObject(result,Goods.class);
                        mView.showGoodsInfo(goods);
                        if(TextUtils.isEmpty(goods.getGoodsName())){
                            ToastUtils.showShort("无法找到货物，请扫描正确的条码");
                        }else {
                            mView.dismissProcess();
                            mView.showMessage("操作成功");
                        }
                    } else {
                        ToastUtils.showShort("服务错误");
                    }
                });
    }

    @Override
    public String getBarCode() {
        return mScanBarCode.scan();
    }

    @Override
    public void getCkList() {
        mChangKuModel.getCkList(
                () -> mView.showMessage("获取仓库信息失败"),
                data -> mView.setCkData(JSON.parseArray(data,String.class))
        );
    }

    @Override
    public void clearLoginInfo() {
        Values.userPhoneNumber = null;
        SPUtils.getInstance().put(Values.SP_CURRENT_USER, "");
        mView.setUserInfo("请登录");
    }
}
